package com.example.smartcity1.service.parklot;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.ParklotListAdapter;
import com.example.smartcity1.bean.ParklotListBean;
import com.example.smartcity1.network.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ParklotListActivity extends AppCompatActivity {

    private ListView listView;
    private Button button;
    private int size = 1;
    private Boolean allOK = false;
    private List<ParklotListBean.RowsDTO> rows = new ArrayList<>();
    private ParklotListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parklot_list);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("停车场");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        adapter = new ParklotListAdapter(ParklotListActivity.this, R.layout.item_parklot_list, rows);
        listView.setAdapter(adapter);
        setData(1);
        button.setOnClickListener(v -> {
            if (!allOK) {
                size += 1;
                setData(size);
            } else if (allOK) {
                Toast.makeText(this, "已全部加载", Toast.LENGTH_SHORT).show();
            }
        });
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(ParklotListActivity.this, ParklotPageActivity.class);
            intent.putExtra("id", rows.get(position).getId());
            startActivity(intent);
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        listView = findViewById(R.id.listView);
        button = findViewById(R.id.button);
    }

    private void setData(int size) {
        RetrofitClient.serviceService().getParklotListData(size).enqueue(new Callback<ParklotListBean>() {
            @Override
            public void onResponse(Call<ParklotListBean> call, Response<ParklotListBean> response) {
                ParklotListBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    rows.addAll(body.getRows());
                    adapter.notifyDataSetChanged();
                    if (rows.size() == body.getTotal()) {
                        allOK = true;
                    }
                }
            }

            @Override
            public void onFailure(Call<ParklotListBean> call, Throwable t) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_parklot_list, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.historyList:
                startActivity(new Intent(ParklotListActivity.this,ParklotOldListActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }
}